<?php
require 'Admin/Model/Model.php';
	class UserModel extends Model{
		function __Construct(){
			parent::__Construct();
            $this->mod = 'users';
		}

		/*
		 * 根据ID查用户信息
		 * */
		function getByID($id){
		    $sql = "select * from users where uid = {$id}";
            $result = $this->conn->query($sql);
            return $result;
        }
        /*
         * 添加用户
         * */
        function add($data){
            if(!empty($_FILES["pic"]["name"])){
                $pic = $this->uploadfile('pic','./upload/',array('jpg','gif','png'));
            }
            else{
                $pic='';
            }

            $user = array(
                'uname' => trim($data['username']),
                'password' => $data['password'],
                'gender' => $data['gender'],
                'birthdate' => $data['birthdate'],
                'hobby' => implode($data['hobby'], ','),
                'degree' => $data['degree'],
                'intro' => $data['intro'],
                'pic'=>$pic
            );
            $keys = implode(',',array_keys($user));
            $values = implode("','", array_values($user));
            $sql="insert into {$this->mod} ({$keys}) VALUES ('{$values}')";
            //执行SQL语句
            $result = $this->conn->query($sql);
            return $result;

        }
        /*
         * 更新用户信息
         * */
        function update($data){
            $user = array(
                'uname' => trim($data['uname']),
                //'password' => $data['password'],
                'gender' => $data['gender'],
                'birthdate' => $data['birthdate'],
                'hobby' => implode($data['hobby'], ','),
                'degree' => $data['degree'],
                'intro' => $data['intro'],
            );
            if(!empty($_FILES["pic"]["name"])){
                $pic = $this->uploadfile('pic','./upload/',array('jpg','gif','png'));
                $user['pic'] = $pic;
            }


            $sets = '';
            foreach ($user as $key=>$val){
                $sets.=$key."='".$val."',";
            }
            $sets=rtrim($sets,','); //去掉SQL里的最后一个逗号

            $sql="UPDATE {$this->mod} SET {$sets} where uid=".$data['id'];
            //执行SQL语句
            $result = $this->conn->query($sql);
            return $result;

        }
        /*
         * 删除用户
         * */
        function del($id){
            $sql = "delete from {$this->mod} where uid=".$id;
            $result = $this->conn->query($sql);
            return $result;
        }
	}
?>